package scoutobahn.xml.automaton;

import java.util.HashMap;

import org.xml.sax.Attributes;

/**
 * A parser state for OSM elements.
 * @author mark
 *
 */
public abstract class OsmElementState extends ParserState {

    private final String name;
    private final OsmItemStreamManager listener;
    private HashMap<String, String> tags;
    /**
     * Constructs a new OsmElementState, with the given StateManager,
     * the given OsmItemStreamManager and the xml element name.
     * @param stateManager The StateManager of this state
     * @param elementName The xml element name of the element
     * @param streamListener The OsmItemStreamManager
     */
    public OsmElementState(final StateManager stateManager,
            final String elementName,
            final OsmItemStreamManager streamListener) {
        super(stateManager);
        this.name = elementName;
        this.listener = streamListener;
        this.tags = new HashMap<String, String>();
    }

    @Override
    public ParserState startElement(final String nameSpace,
            final String localName,
            final String qName, final Attributes attr) {
        if (localName.equals("tag")) {
            this.tags.put(attr.getValue("k"), attr.getValue("v"));
        }
        return this;
    }
    @Override
    public ParserState endElement(final String uri,
            final String localName, final String qName) {
        if (localName.equals(this.getElementName())) {
            return super.getStateManager().getUndefinedState();
        }
        return this;
    }
    /**
     * Returns the name of the element.
     * @return Element name
     */
    public String getElementName() {
        return this.name;
    }
    /**
     * Returns the OsmItemStreamManager.
     * @return The OsmItemStreamManager
     */
    public OsmItemStreamManager getItemStreamListener() {
        return this.listener;
    }
    /**
     * Return the Map containing all tags of the osm element.
     * @return Map of tags
     */
    protected HashMap<String, String> getTags() {
        return this.tags;
    }

}
